﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game_Engine
{
    public class GameObject
    {                                            
              
        protected List<Component> components;

        public Transform Transform{ get; set;}
        public List<Component> Components { get { return components; } }

        public GameObject() : base()
        {
            components = new List<Component>();
        }
        
        public virtual void Add(Component component)
        {
            Component c = GetComponent(component.GetType());
            if (c != null)
            {
                components.Remove(c);
            }
            components.Add(component);
            component.Host = this;
        }

        public Component GetComponent(String name)
        {
            foreach (Component component in components)
                if(component.Name == name)
                    return component;
            return null;
        }

        public Component GetComponent(Type type)
        {
            foreach (Component component in components)
                if (component.GetType() == type)
                    return component;
            return null;
        }
        
        public virtual void Update() 
        {
            /*CharacterController cc = (CharacterController)GetComponent(typeof(CharacterController));
            if (cc != null)
                cc.Update();
            else*/
            {
                Rigidbody rb = (Rigidbody)GetComponent(typeof(Rigidbody));
                if (rb != null)
                    rb.Update();
            }
        }
    }
}
